<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Visual Basic Basics</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Visual Basic, learn Visual Basic, basics, tutorial, programming language">
<meta name="description" content="This part of the Visual Basic tutorial covers 
the basics of Visual Basic language.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Basics</h1>

<p>
In this part of the Visual Basic tutorial, we will cover basic 
programming concepts of the Visual Basic language. We introduce 
the very basic programs. We will work with variables, constants 
and basic data types. We will read and write to the console; 
we will mention variable interpolation.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>

<p>
The following is a simple Visual Basic program.
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Console.WriteLine("This is Visual Basic")

    End Sub

End Module 
</pre>

<p>
We will explain the program line by line. 
</p>

<pre class="explanation">
Option Strict On
</pre>

<p>
<code>Option Strict</code> statement can be either On or Off.
The default is Off, for backward compatibility with Older Visual Basic programs.
This statement is recommended in all your new programs. When the statement is On,
the compiler will detect various bad programming practices.
</p>

<pre class="explanation">
Module Example
    ...
End Module 
</pre>

<p>
Each Visual Basic program should be properly structured. The source code is placed into
modules. Within modules, the code is further divided into procedures and functions. 
A properly structured module is easier to maintain and is less error prone. 
In our case, we create a module called Example. For this, we use the 
<code>Module</code> keyword. A module definition is 
ended with the <code>End Module</code> statement. 
</p>

<pre class="explanation">
Sub Main()
    ...
End Sub
</pre>

<p>
With a <code>Sub</code> keyword, we create a procedure. Sub is derived
from the subroutine word. Subroutines and procedures are synonyms. In Visual Basic,
the preferred word is the procedure. A procedure is a block of Visual Basic
code. This is to achieve modularity of the code. 
</p>

<pre class="explanation">
Console.WriteLine("This is Visual Basic")
</pre>

<p>
In this code line, we print the "This is Visual Basic" string
literal to the console. To print a message to the console, we use the 
<code>WriteLine()</code> method of the <code>Console</code> class.
It represents the standard input, output, and error streams 
for console applications.  
</p>

<pre>
$ ./simple.exe 
This is Visual Basic
</pre>

<p>
Executing the program gives the above output.
</p>

<hr class="btm">

<p>
We can use a Console class to read values as well. 
</p>

<pre class="code">
Option Strict On

Module Example

    Dim name As String

    Sub Main()

        Console.WriteLine("Enter your name: ")
        name = Console.ReadLine()
        Console.WriteLine("Hello {0}", name)

    End Sub

End Module 
</pre>

<p>
The second program will read a value from a console
and print it. 
</p>

<pre class="explanation">
Dim name As String
</pre>

<p>
The <code>Dim</code> keyword is used to declare a 
variable. The variable is called 'name'. Unlike constants, which
store only one value during the life of the program, variables
may store various different values. The <code>As</code>
keyword defines the data type of the variable. Our variable
will hold string values.
</p>

<pre class="explanation">
name = Console.ReadLine()
</pre>

<p>
We read a line from the terminal. When we hit the Enter, the 
string is assigned to the name variable. 
</p>


<pre class="explanation">
Console.WriteLine("Hello {0}", name)
</pre>

<p>
In this code line, we perform variable interpolation. <b>Variable interpolation</b>
is replacing variables with their values inside string literals. 
Another names for variable interpolation are: variable substitution and variable expansion.
</p>

<pre>
$ ./readline.exe 
Enter your name: 
Jan
Hello Jan
</pre>

<p>
This is the output of the second program.
</p>


<h2>Command line arguments</h2>

<p>
Visual Basic programs can receive command line arguments. 
There are several ways to retrieve arguments from the
command line. 
</p>

<pre class="code">
Option Strict On

Module Example

    Dim cline As String

    Sub Main()

        cline = Command()
        Console.WriteLine(cline)

    End Sub

End Module 
</pre>

<p>
In the above program, we get the command line arguments
and print them to the terminal. 
</p>

<pre class="explanation">
cline = Command()
</pre>

<p>
The <code>Command()</code> function is used to get the arguments. 
</p>

<pre class="explanation">
Console.WriteLine(cline)
</pre>

<p>
We print the command line arguments to the terminal. 
</p>

<pre>
$ ./commandargs.exe 1 2 3
/home/vronskij/programming/basic/basics/commandargs.exe 1 2 3
</pre>

<p>
We execute the program followed by three numbers. The program prints
the numbers as well as the program name. Program name is the first
command line argument. 
</p>

<p>
In Visual Basic 2008 Express Edition, select project properties. 
In the Debug tab, there is a text area for specifying the command
line arguments. 
</p>

<img src="/img/lang/vb/arguments.png" alt="Command line arguments">
<div class="figure">Figure: Command line arguments</div>

<hr class="btm">

<pre class="code">
Option Strict On

Module Example

    Dim size As Integer

    Sub Main(ByVal cmdArgs() As String)

        size = cmdArgs.Length
        
        If size > 0 Then
            For i As Integer = 0 To size - 1
                Console.WriteLine(cmdArgs(i))
            Next
        End If

    End Sub

End Module 
</pre>

<p>
Command line arguments can be passed to the 
Main procedure. 
</p>

<pre class="explanation">
Sub Main(ByVal cmdArgs() As String)
</pre>

<p>
This Main() procedure receives a string array of 
command line arguments. 
</p>

<pre class="explanation">
size = cmdArgs.Length
</pre>

<p>
We determine the size of the array. 
</p>

<pre class="explanation">
If size > 0 Then
    For i As Byte = 0 To size - 1
        Console.WriteLine(cmdArgs(i))
    Next
End If
</pre>

<p>
We go through the array and print all arguments to the console.
Note that in this case, the name of the program is not included
among the arguments. 
</p>

<pre>
$ ./cmdargs.exe 2 3 5
2
3
5
</pre>

<p>
We provide three numbers as command line arguments and these are
printed to the console. 
</p>


<h2>Variables and Constants</h2>

<p>
A <b>variable</b> is a place to store data. A variable
has a name and a data type. A data type determines, what values can
be assigned to the variable. Integers, strings, boolean values etc.
Over the time of the program, variables can obtain various values
of the same data type. Variables are always initialized to the
default value of their type before any reference to the variable 
can be made. Variables are declared with the <code>Dim</code>
keyword. Unlike variables, <b>constants</b> retain their values. Once
initialized, they cannot be modified. Constants are created with 
the <code>Const</code> keyword. 
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()
    
        Dim city As String = "New York"

        Dim name As String = "Paul", age As Integer = 35, _
            nationality As String = "American"

        Console.WriteLine(city)
        Console.WriteLine(name)
        Console.WriteLine(age)
        Console.WriteLine(nationality)
        
        city = "London"
        Console.WriteLine(city)

    End Sub

End Module
</pre>

<p>
In the above example, we work with four variables. 
</p>

<pre class="explanation">
Dim city As String = "New York"
</pre>

<p>
We declare a city variable of the String type and
initialize it to the "New York" value.
</p>

<pre class="explanation">
Dim name As String = "Paul", age As Integer = 35, _
    nationality As String = "American"
</pre>

<p>
We can declare and initialize more variables with one
<code>Dim</code> keyword; they are separated 
with a comma character. 
</p>

<pre class="explanation">
Console.WriteLine(city)
Console.WriteLine(name)
Console.WriteLine(age)
Console.WriteLine(nationality)
</pre>

<p>
We print the values of the variables to the terminal.
</p>

<pre class="explanation">
city = "London"
</pre>

<p>
We assign a new value to the city variable. 
</p>


<hr class="btm">

<p>
As we already said above, constants cannot change their
initial values. 
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()
        
        Const WIDTH As Integer = 100
        Const HEIGHT As Integer = 150
        Dim var As Integer = 40

        var = 50

        Rem WIDTH = 110

    End Sub

End Module
</pre>

<p>
In this example, we declare two constants and one variable.
</p>

<pre class="explanation">
Const WIDTH As Integer = 100
Const HEIGHT As Integer = 150
</pre>

<p>
We use the <code>Const</code> keyword
to inform the compiler, that we declare a constant. It
is a convention to write constants in upper case letters.
</p>

<pre class="explanation">
Dim var As Integer = 40

var = 50
</pre>

<p>
We declare and initialize a variable. Later, we assign a new value
to the variable.
</p>

<pre class="explanation">
Rem WIDTH = 110
</pre>

<p>
This is not possible with a constant. If we uncomment this line,
we will get a compilation error. 
</p>



<h2>Variable interpolation</h2>

<p>
<b>Variable interpolation</b> is replacing
variables with their values inside string literals.
Another names for variable interpolation are: variable 
substitution and variable expansion.
</p>

<pre class="code">
Option Strict On

Module Example
    
    Dim age As Byte = 34
    Dim name As String = "William"
    Dim output As String

    Sub Main()
        
        output = String.Format("{0} is {1} years old.", _
            name, age)
       
        Console.WriteLine(output)

    End Sub

End Module
</pre>

<p>
In Visual Basic, strings are immutable. We cannot modify an existing
string. Variable interpolation happens during string creation. 
</p>


<pre class="explanation">
Dim age As Byte = 34
Dim name As String = "William"
Dim output As String
</pre>

<p>
Here we declare three variables. 
</p>

<pre class="explanation">
output = String.Format("{0} is {1} years old.", _
    name, age)
</pre>

<p>
We use the <code>Format()</code> method of the
built-in String module. The {0}, {1} are the places, where
the variables are evaluated. The numbers represent the 
position of the variable. {0} evaluates to the first
supplied variable, {1} to the second etc.
</p>

<pre>
$ ./interpolation.exe 
William is 34 years old.
</pre>

<p>
Output.
</p>


<p>
This chapter covered some basics of the Visual Basic language.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>

<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified August 15, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

